package cn.cdiothree.service.impl;

import cn.cdiothree.dao.OrderCommentMapper;
import cn.cdiothree.dataobject.OrderComment;
import cn.cdiothree.enums.ResultEnum;
import cn.cdiothree.exception.SellException;
import cn.cdiothree.service.OrderCommentService;
import cn.cdiothree.util.KeyUtil;
import cn.cdiothree.vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品图文评论Service
 */
@Service
@Slf4j
public class OrderCommentServiceImpl extends ServiceImpl<OrderCommentMapper, OrderComment> implements OrderCommentService {
    @Autowired
    OrderCommentMapper orderCommentMapper;

    /**
     * 添加文字评论
     * @param userId
     * @param orderId
     * @param commentText
     * @return
     */
    @Override
    public ResultVO addTextComment(Integer userId, String orderId, String commentText){
        OrderComment orderComment = new OrderComment();
        orderComment.setCommentId(KeyUtil.genUniqueKey());
        orderComment.setUserId(userId);
        orderComment.setOrderId(orderId);
        orderComment.setCommentText(commentText);
//        orderComment.setImgUrl(imgUrl);
        orderComment.setCreateTime(LocalDateTime.now());
        orderComment.setUpdateTime(LocalDateTime.now());
        int insert = orderCommentMapper.insert(orderComment);
        log.info("用户{}为订单{}添加评论",userId,orderId);
        if (insert<1){
            throw new SellException(ResultEnum.ADD_ORDER_COMMENT_FAILED);
        }
        return this.getTextComment(userId,orderId);
    }


    /**
     * 获取文字评论
     * @param userId
     * @param orderId
     * @return
     */
    @Override
    public ResultVO getTextComment(Integer userId,String orderId){
        log.info("获取用户{}为订单{}评论信息",userId,orderId);
        QueryWrapper<OrderComment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("order_id",orderId);
        List<OrderComment> orderComment = orderCommentMapper.selectList(wrapper);
        return ResultVO.success(orderComment);
    }

    /**
     * 获取所有评论信息
     * @param userId
     * @param orderId
     * @return
     */
    @Override
    public List<OrderComment> getCommentList(Integer userId, String orderId){
        log.info("获取用户{}为订单{}评论所有信息",userId,orderId);
        QueryWrapper<OrderComment> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("order_id",orderId);
        return orderCommentMapper.selectList(wrapper);
    }
}
